#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<vector<int>> threeSum(vector<int> &nums)
{
    sort(nums.begin(), nums.end());
    vector<vector<int>> vv;
    int cur = 0;
    while (cur < nums.size() && nums[cur] <= 0)
    {
        int left = cur + 1;
        int right = nums.size() - 1;
        while (left < right)
        {
            int sum = nums[left] + nums[right];
            int target = -nums[cur];
            if (sum < target)
            {
                left++;
            }
            else if (sum > target)
            {
                right--;
            }
            else
            {
                vv.push_back({nums[cur], nums[left], nums[right]});
                left++, right--;
                while (left < right && nums[left] == nums[left - 1])
                {
                    left++;
                }
                while (left < right && nums[right] == nums[right + 1])
                {
                    right--;
                }
            }
        }
        cur++;
        while (cur < nums.size() && nums[cur] == nums[cur - 1])
        {
            cur++;
        }
    }
    return vv;
}
